<?php
namespace Common\Controller;
use Common\Controller\CommonController;
class YaoController extends CommonController
{
    //更新游戏结果
    public function update_playerScore($now_level = 0,$old_level = 0)
    {
        $field           =   array('id','min_level','max_level','sum');
        $config_data     =   M('colour_config')->field($field)->select();
        if (! empty($config_data))
        {
            //先减去再加上
            /*
            if (! empty($old_level))
            {
                foreach ($config_data as $val)
                {
                    if ($val['min_level'] <= $old_level && $old_level <= $val['max_level'] && $val['sum'] >= 1)
                    {
                        $update_data        =   array();
                        $update_data['sum'] =   $val['sum'] - 1;
                        M('colour_config')->where(array('id'=>$val['id']))->data($update_data)->save(); // 根据条件保存修改的数据
                        continue;
                    }
                }
            }
            */

            if (! empty($now_level))
            {
                foreach ($config_data as $val)
                {
                    if ($val['min_level'] <= $now_level && $now_level <= $val['max_level'])
                    {
                        $update_data        =   array();
                        $update_data['sum'] =   $val['sum'] + 1;
                        M('colour_config')->where(array('id'=>$val['id']))->data($update_data)->save(); // 根据条件保存修改的数据
                        continue;
                    }
                }
            }

        }
    }

    //绑定好友关系
    public function make_friend($my_member_id ,$share_uid)
    {
        $field          =   array('colour_member_id');
        $relationship   =   M('colour_member')->where(array('third'=>$share_uid))->field($field)->find();
        if (! empty($relationship))
        {
            if ($relationship['colour_member_id'] != $my_member_id)
            {
                $where_friend                   =   array();
                $where_friend['my_member_id']   =   $my_member_id;
                $where_friend['coupling_member_id']    =   $relationship['colour_member_id'];
                $relation_result                =   M('colour_relationship')->where($where_friend)->field(array('colour_relationship_id'))->find();
                if (empty($relation_result))
                {
                    $insert_data                        =   array();
                    $insert_data['my_member_id']        =   $my_member_id;
                    $insert_data['coupling_member_id']  =   $relationship['colour_member_id'];
                    $insert_data['createtime']          =   time();
                    M('colour_relationship')->add($insert_data);
                }
            }

        }
    }

}